package account

import (
	"errors"
	"fmt"
	"net/http"

	"com.jiaguyu/session"
	"github.com/gin-gonic/gin"
)

//账号 中间件  判断账号用 的

func SetUserId(userId int64, ctx *gin.Context) {
	var userSession session.Session
	temSession, exist := ctx.Get(SessionName)
	if !exist {
		return
	}
	userSession, ok := temSession.(session.Session)
	if !ok {
		return
	}

	if userSession == nil {
		return
	}
	fmt.Println("userid", userId)

	//将sessionid设置到session中
	userSession.Set(SessionUserId, userId)

}

//用户登录请求
func ProcessRequest(ctx *gin.Context) {
	var userSession session.Session
	fmt.Println("开始请求是据")
	defer func() {
		if userSession == nil {
			userSession, _ = session.CreateSession()
		}
		ctx.Set(SessionName, userSession)
	}()
	cookie, err := ctx.Request.Cookie(CookieSessionId)
	fmt.Println("cookies", cookie, err)
	if err != nil {
		//没有读到用户cookies
		ctx.Set(SessionUserId, int64(0))
		ctx.Set(GinUserLoginStatus, int64(0))
		return
	}
	sessionId := cookie.Value
	fmt.Println("cookies value", sessionId)
	if len(sessionId) == 0 {
		ctx.Set(SessionUserId, int64(0))
		ctx.Set(GinUserLoginStatus, int64(0))
		return
	}

	//获取sessionID
	userSession, err = session.Get(sessionId)

	fmt.Println("userSession value", userSession, err)

	if err != nil {
		ctx.Set(SessionUserId, int64(0))
		ctx.Set(GinUserLoginStatus, int64(0))
		return
	}

	temUserId, err := userSession.Get(SessionUserId)
	fmt.Println("temUserId ", temUserId, err)
	if err != nil {
		ctx.Set(SessionUserId, int64(0))
		ctx.Set(GinUserLoginStatus, int64(0))
		return
	}
	userId, ok := temUserId.(int64)
	fmt.Println("userId ", userId, ok)
	if !ok || userId == 0 {
		ctx.Set(SessionUserId, int64(0))
		ctx.Set(GinUserLoginStatus, int64(0))
		return
	}
	//获取到userid 登陆ok
	ctx.Set(SessionUserId, int64(userId))
	ctx.Set(GinUserLoginStatus, int64(1))
	GinUserLoginStatusss, _ := ctx.Get(GinUserLoginStatus)
	GinUserLoginStatusss1, _ := ctx.Get(SessionUserId)
	fmt.Println("GinUserLoginStatusss", GinUserLoginStatusss)
	fmt.Println("GinUserLoginStatusss1", GinUserLoginStatusss1)
	return
}

func GetUserId(ctx *gin.Context) (userId int64, err error) {
	tempUserId, exist := ctx.Get(SessionUserId)

	if !exist {
		err = errors.New("not esixts")
		return
	}
	userId, ok := tempUserId.(int64)
	if !ok {
		err = errors.New("user is recover to int 64 failed")
		return
	}
	return
}

func IsLogin(ctx *gin.Context) (login bool) {
	tempLoginStatus, boolExit := ctx.Get(GinUserLoginStatus)
	fmt.Printf("tempLoginStatus,:%v--boolExit:%v,\n", tempLoginStatus, boolExit)
	if !boolExit {
		return
	}
	loginStatus, ok := tempLoginStatus.(int64)
	fmt.Printf("loginStatus,:%v,ok:%v\n", loginStatus, ok)
	if !ok {
		//err = errors.New("user is recover to int 64 failed")
		return
	}
	if loginStatus == 0 {
		return
	}
	//登陆
	login = true
	return
}

func ProcessResponse(ctx *gin.Context) {
	var userSession session.Session
	temSession, exist := ctx.Get(SessionName)
	if !exist {
		return
	}
	userSession, ok := temSession.(session.Session)
	if !ok {
		return
	}
	if userSession == nil {
		return
	}
	fmt.Printf("get is succ/n")
	fmt.Println("get is succ/n,", userSession.IsModify())
	if userSession.IsModify() == false {
		return
	}
	err := userSession.Save()
	if err != nil {
		return
	}
	fmt.Printf("save is succ/n")
	//种cookies
	sessionId := userSession.Id()
	cookie := &http.Cookie{
		Name:     CookieSessionId,
		Value:    sessionId,
		MaxAge:   CookieMaxAge,
		HttpOnly: true,
		Path:     "/",
	}
	http.SetCookie(ctx.Writer, cookie)
	fmt.Printf("set cookie  succ\n")
	return
}
